import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/a_yyt/page/health_record_page.dart';
import 'package:ls_work/a_yyt/page/health_regist_page.dart';
import 'package:ls_work/res/colors.dart';

class HealthHomePage extends StatefulWidget {
  final ElderInfo? info;

  HealthHomePage({this.info});

  @override
  _HealthHomePageState createState() => _HealthHomePageState();
}

class _HealthHomePageState extends State<HealthHomePage> with SingleTickerProviderStateMixin {
  TabController? _tabController;
  PageController _pageController = PageController(initialPage: 0);
  @override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight
    ]);
    _tabController = new TabController(vsync: this, length: 2);
  }

  @override
  void dispose() {
    _tabController?.dispose();
    super.dispose();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,

    ]);
  }

  _onPageChange(int index) {
    _tabController?.animateTo(index);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.5,
        title: TabBar(
          onTap: (index) {
            if (!mounted) {
              return;
            }
            _pageController.jumpToPage(index);
          },
          isScrollable: false,
          controller: _tabController,
          labelStyle: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
          unselectedLabelStyle:
              TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
          indicatorSize: TabBarIndicatorSize.label,
          unselectedLabelColor: Colours.text_normal,
          labelColor: Colors.black,
          indicatorColor: Colours.app_main,
          indicatorWeight: 5,
          tabs: <Widget>[
            Tab(text: '健康登记',),
            Tab(text: '健康记录',),
          ],
        ),
      ),
      body: PageView.builder(
        key: const Key('pageView'),
        itemCount: 2,
        onPageChanged: _onPageChange,
        controller: _pageController,
        itemBuilder: (BuildContext context, int index) {
          switch (index) {
            case 0:
              return HealthRegistPage(info: widget.info,);
              break;
              case 1:
              return HealthRecordPage(info: widget.info,);
              break;
            default:
            return Container();
          }
        },
      ),
    );
  }
}
